home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0597.zip / TECHTIPS.ZIP / BT.C next >
C/C++ Source or Header  |  1997-02-24  |  1KB  |  60 lines

  1. // note - make sure the multi-threaded library option
  2. // is on in your compiler's IDE settings
  3.  
  4. #include <windows.h>
  5. #include <process.h>
  6. #include <stdio.h>
  7.  
  8. HANDLE beginthread_handle(
  9. #ifdef __BORLANDC__
  10.    void _USERENTRY (*start_address)(void *),
  11. #else
  12.    unsigned (__stdcall *start_address)(void *),
  13. #endif
  14.    unsigned stack_size,
  15.    void *arglist )
  16. {
  17.    DWORD id;
  18.    HANDLE h1 = (HANDLE)
  19. #ifdef __BORLANDC__
  20.         _beginthreadNT(start_address,stack_size,
  21.                 arglist,NULL,CREATE_SUSPENDED,&id);
  22. #else
  23.         // for Visual C++ and compatibles
  24.         _beginthreadex(NULL,stack_size,start_address,
  25.                arglist,CREATE_SUSPENDED,&id);
  26. #endif
  27.    HANDLE h2 = INVALID_HANDLE_VALUE;
  28.    if( h1 != INVALID_HANDLE_VALUE ) {
  29.       DuplicateHandle(GetCurrentProcess(),h1,
  30.           GetCurrentProcess(),&h2,0,FALSE,
  31.           DUPLICATE_SAME_ACCESS);
  32.       ResumeThread(h1);
  33.    }
  34.    return h2;
  35. }
  36.  
  37. #ifdef __BORLANDC__
  38. void thread( void *arg )
  39. #else
  40. unsigned __stdcall thread( void * arg )
  41. #endif
  42. {
  43.    printf("new thread called\n");
  44. #ifndef __BORLANDC__
  45.    return 0;
  46. #endif
  47. }
  48.  
  49. int main(void)
  50. {
  51.    HANDLE h = beginthread_handle(thread,4096,NULL);
  52.    if(h!=INVALID_HANDLE_VALUE) {
  53.        printf("waiting on new thread\n");
  54.        WaitForSingleObject(h,INFINITE);
  55.        printf("new thread is signaled\n");
  56.        CloseHandle(h);
  57.    }
  58.    return 0;
  59. }
  60.